package com.zhuoyue.common.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zhuoyue.common.exception.ErrorType;
import com.zhuoyue.common.exception.ZyServerException;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @author wjming
 */
public class JsonUtils {
    public static Map<String, Object> jsonToMap(String jsondata) {
        try {
            JSONObject obj = JSON.parseObject(jsondata);
            //map对象
            Map<String, Object> data = new HashMap<>(5);
            //循环转换
            Iterator it = obj.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry<String, Object> entry = (Map.Entry<String, Object>) it.next();
                data.put(entry.getKey(), entry.getValue());
            }
            return data;
        } catch (Exception e) {
            throw new ZyServerException(ErrorType.INVALID_PARAMS.getCode(), "json格式不正确");
        }

    }

    public static Map<Object, Object> jsonToMapObject(String jsondata) {
        JSONObject obj = JSON.parseObject(jsondata);
        //map对象
        Map<Object, Object> data = new HashMap<>(5);
        //循环转换
        Iterator it = obj.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<Object, Object> entry = (Map.Entry<Object, Object>) it.next();
            data.put(entry.getKey(), entry.getValue());
        }
        return data;
    }

    /**
     * Map转Object对象
     *
     * @param map
     * @param beanClass
     * @return 对象
     * @throws Exception
     */
    public static Object mapToObject(Map<Object, Object> map, Class<?> beanClass) throws Exception {
        if (map == null){
            return null;
        }
        Object obj = beanClass.newInstance();
        Field[] fields = obj.getClass().getDeclaredFields();
        for (Field field : fields) {
            int mod = field.getModifiers();
            if (Modifier.isStatic(mod) || Modifier.isFinal(mod)) {
                continue;
            }
            field.setAccessible(true);
            if (map.containsKey(field.getName())) {
                field.set(obj, map.get(field.getName()));
            }
        }
        return obj;
    }


    public static String getAcSwitch(String controlParams) {
        JSONObject jsonObject = JSONObject.parseObject(controlParams);
        return jsonObject.getString("acSwitch");
    }

    public static String getAcMode(String controlParams) {
        return JSONObject.parseObject(controlParams).getString("acMode");
    }

    public static String getAcWindSpeed(String controlParams) {
        return JSONObject.parseObject(controlParams).getString("acWindSpeed");
    }

    public static String getAcWindDirection(String controlParams) {
        return JSONObject.parseObject(controlParams).getString("acWindDirection");
    }

    public static int getAcTemp(String controlParams) {
        return JSONObject.parseObject(controlParams).getInteger("acTemp");
    }
}
